﻿namespace FamilyTree_v1._0
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Linq;
    using DevExpress.XtraEditors;
    using System.Collections;
    using DevExpress.XtraGrid.Views.Grid;
    using DevExpress.XtraGrid.Views.Base;

    public partial class frmMain : DevExpress.XtraEditors.XtraForm
    {
        ConstructionGenealogy ucConstructionGenealogy = new ConstructionGenealogy();
        public static FamilyTree tree;
        DataFamilyTreeDataContext dataFamilyContext;
        public frmMain()
        {
            InitializeComponent();
            tree= new FamilyTree();
            dataFamilyContext = new DataFamilyTreeDataContext();
        }

        private void barBtnFullGenealogyShow_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            splitContainerControl1.Panel2.Controls.Clear();
            DisplayGenealogy ucDisplayGenealogy = new DisplayGenealogy();
            ucDisplayGenealogy.Dock = DockStyle.Fill;
            splitContainerControl1.Panel2.Controls.Add(ucDisplayGenealogy);
        }
       
        
        private void barBtnGenealogyConstruction_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ConstructionGenealogy_Process();
        }

        private void barBtnGenealogyShow_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            splitContainerControl1.Panel2.Controls.Clear();
            DisplayGenealogy ucDisplayGenealogy = new DisplayGenealogy();
            ucDisplayGenealogy.Dock = DockStyle.Fill;
            splitContainerControl1.Panel2.Controls.Add(ucDisplayGenealogy);
        }
        void ConstructionGenealogy_Process()
        {
            splitContainerControl1.Panel2.Controls.Clear();
            splitContainerControl1.Panel2.Controls.Add(ucConstructionGenealogy);
            ucConstructionGenealogy.Dock = DockStyle.Fill;     
        }
        private void splitContainerControl1_Panel2_Resize(object sender, EventArgs e)
        {
            ucConstructionGenealogy.ResizePanel(splitContainerControl1.Panel2.Width, splitContainerControl1.Panel2.Height);
        }
        private void frmMain_Load(object sender, EventArgs e)
        {
            var arrPerson = from p in dataFamilyContext.Persons
                            select new { f1 = p.Sex, f2 = p.LastName + " " + p.MiddleName + " " + p.FirstName, p.Generation, p.DateOfBirth, p.DateOfDeath };
            gridControl1.DataSource = arrPerson.ToList();
            gridView1.Columns.Clear();
            string[] fields = { "f1", "f2", "Generation", "DateOfBirth", "DateOfDeath" };
            string[] captions = { "GT", "Họ và Tên", "Đời", "Năm Sinh", "Năm Mất" };
            for (int i = 0; i < fields.Length; i++)
            {
                DevExpress.XtraGrid.Columns.GridColumn col = new DevExpress.XtraGrid.Columns.GridColumn();
                col.FieldName = fields[i];
                col.Caption = captions[i];
                col.Resize(300);
                gridView1.Columns.Add(col);
                gridView1.Columns[i].Visible = true;
            }

            gridView1.BestFitColumns();
            gridView1.OptionsView.ColumnAutoWidth = false;
            gridView1.ScrollStyle = ScrollStyleFlags.LiveHorzScroll;
            gridView1.HorzScrollVisibility = ScrollVisibility.Always;    
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            dataFamilyContext.Connection.Close();
        }

    }
}